package com.cwd.arm.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * Created by yan on 21-11-26.
 */
@Slf4j
public class DateUtil {

    private static final int BEGIN_YEAR = 2000;

    private static final String SDF_YYYY_MM = "yyyy-MM";

    private static final String SDF_YYYY_MM_DD = "yyyy-MM-dd";

    private static final String SDF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static Map<String, List<Integer>> qMap;

    public static final int Q1 = 1;
    public static final int Q2 = 2;
    public static final int Q3 = 3;
    public static final int Q4 = 4;


    public static int getThisFy() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    public static int getThisMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    public static int getNextMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        return cal.get(Calendar.MONTH) + 1;
    }

    public static List<Integer> getFyList() {
        List<Integer> fyList = new ArrayList<>();
        int endYear = getThisFy() + 2;
        for (int i = BEGIN_YEAR; i < endYear; i++) {
            fyList.add(i);
        }
        return fyList;
    }

    public static List<Integer> getPlanMonthList() {
        List<Integer> mList = new ArrayList<>();
        mList.add(getThisMonth());
        mList.add(getNextMonth());
        return mList;
    }

    /**
     * 得到当前季度
     *
     * @return
     */
    public static int getThisQ() {
        return getQ(getThisMonth());
    }

    /**
     * 得到本月季度
     *
     * @return
     */
    public static int getQ(Integer month) {
        int q = 1;
        Map<String, List<Integer>> m = getQMap();
        for (Map.Entry<String, List<Integer>> entry : m.entrySet()) {
            if (entry.getValue().contains(month)) {
                return Integer.parseInt(entry.getKey());
            }
        }
        log.error("没有找到对应的季度");
        return q;
    }

    public static Map<String, List<Integer>> getQMap() {
        if (qMap == null) {
            qMap = new HashMap<>();
            qMap.put("1", Arrays.asList(1, 2, 3));
            qMap.put("2", Arrays.asList(4, 5, 6));
            qMap.put("3", Arrays.asList(7, 8, 9));
            qMap.put("4", Arrays.asList(10, 11, 12));
        }
        return qMap;
    }


    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    public static String dateToStr(Date date, String sdfPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(sdfPattern);
        return sdf.format(date);
    }

    public static String dateToTimeStr(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYY_MM_DD_HH_MM_SS);
        return sdf.format(date);
    }

    public static String dateToDateStr(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYY_MM_DD);
        return sdf.format(date);
    }

    public static Date strToDate(String dateStr) throws Exception {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(dateStr);
    }

    public static int getYear(Date date) {
        String dateStr = dateToStr(date, SDF_YYYY_MM);
        return Integer.parseInt(dateStr.substring(0, 4));
    }

    public static int getMonth(Date date) {
        String dateStr = dateToStr(date, SDF_YYYY_MM);
        return Integer.parseInt(dateStr.substring(5, 7));
    }

    /**
     * 为同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isTheSameDate(Date date1, Date date2) {
        if (differentDaysByMillisecond(date1, date2) == 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前时间的一周时间
     *
     * @return
     */
    public static List<Date> getNextWeekDays() {
        LocalDate localDate = LocalDate.now().plusWeeks(1);
        return getWeekDays(localDate);
    }

    /**
     * 获取当前时间的一周时间
     *
     * @return
     */
    public static List<Date> getWeekDays(LocalDate localDate) {
        List<Date> dateList = new ArrayList<>(7);
        LocalDate day1 = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        dateList.add(localDate2Date(day1));
        LocalDate preDay = day1;
        for (int i = 0; i < 6; i++) {
            LocalDate day = preDay.plusDays(1);
            dateList.add(localDate2Date(day));
            preDay = day;
        }
        return dateList;
    }


    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());

    }

    /**
     * 判断date1是不是在date2之前
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isBeforeDate(Date date1, Date date2) {
        if (date1.before(date2)) {
            return true;
        }
        return false;
    }

}
